#if WINDOWS
using Microsoft.Maui.Platform;
using PInvoke;
using static PInvoke.User32;
using MicrosoftuiWindowing = Microsoft.UI.Windowing;
using MicrosoftuiXaml = Microsoft.UI.Xaml;
using Microsoftui = Microsoft.UI;
using MicrosoftuixmlMedia = Microsoft.UI.Xaml.Media;
#endif
using System.Windows.Input;
using Tester.Models;

namespace Tester;

public partial class NavPage : TabbedPage
{
    public Views.CommonConfig CommonConfig { get; set; }
    public NavPage()
    {
        InitializeComponent();
        this.BindingContext = new ViewModels.NavViewModels();
        //this.BindingContext = new MainViewModels();
        Message.Register(ShowMessage, "ShowDialog");
        Message.Register(ResizeWindow, "ResizeWindow");
        Navigate.Register(Navi, "Navigate");

        //#if WINDOWS
        //        ResizeWindow();
        //#endif
        //NavigateCommand = new Command<Type>(
        //               async (Type pageType) =>
        //               {
        //                   Page page = (Page)Activator.CreateInstance(pageType);
        //                   await Navigation.PushAsync(page);
        //               });

        //BindingContext = this;
    }

    private async void ShowMessage(string title, string details, string button)
    {
        await DisplayAlert(title, details, button);
    }

    private async void ShowMessageMulti(string title, string details, string accept, string cancel)
    {
        await DisplayAlert(title, details, accept, cancel);
    }

    private async void Navi(Type pageType)
    {
        Page page = (Page)Activator.CreateInstance(pageType);
        await Navigation.PushAsync(page);
        BindingContext = this;
    }

    private void ResizeWindow()
    {
#if WINDOWS

        var winuiWindow = Window.Handler?.PlatformView as MicrosoftuiXaml.Window;
        if (winuiWindow is null)
            return;
        var appWindow = winuiWindow.GetAppWindow();
        if (appWindow is null)
            return;

        var displyArea = MicrosoftuiWindowing.DisplayArea.Primary;
        double scalingFactor = winuiWindow.GetDisplayDensity();
        var width = 800 * scalingFactor;
        var height = 600 * scalingFactor;
        double startX = (displyArea.WorkArea.Width - width) / 2.0;
        double startY = (displyArea.WorkArea.Height - height) / 2.0;

        appWindow.MoveAndResize(new((int)startX, (int)startY, (int)width, (int)height), displyArea);

        //appWindow.TitleBar?.ResetToDefault();
#endif
    }
   
}